package controller;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import service.MemberService;
import util.Utilities;
import dto.MemberDTO;

@Controller
public class MemberController {
	private MemberService service;
	
	public MemberController() {
		super();
	}
	public MemberController(MemberService service) {
		this.service = service;
	}
	@RequestMapping("register_form.do")
	public String registerFormController(){
		return "register_form.jsp";
	}
	@RequestMapping("login_form.do")
	public String loginFormController(){
		return "login_form.jsp";
	}
	// 회원가입
	@RequestMapping(value="register.do", method = RequestMethod.POST)
	public ModelAndView registerController(HttpServletRequest request, 
			HttpServletResponse response, MemberDTO mdto) throws Exception{
		
		try{
			// 생년월일 변환
			String year = request.getParameter("birth_year");
			String month = request.getParameter("birth_month"); 
			String day = request.getParameter("birth_day");
			if(month.length()!=2){
				month = "0".concat(month);
			}
			if(day.length()!=2){
				day = "0".concat(day);
			}
			mdto.setBirthday(year+month+day);
			// 가입 시간 저장
			System.out.println(mdto);
			String regist_date = Utilities.getNowForMember();
			mdto.setRegist_date(regist_date);
			mdto.setDateFordisplay(Utilities.changeDayFormat(regist_date));
			service.registerMember(mdto);
		}catch (Exception e) {
			e.printStackTrace();
			new ModelAndView("member.res","error_message",e.getMessage());
		}		
		return new ModelAndView("registerSuccess.res","mdto",mdto); 
	}
	// 로그인
	@RequestMapping("login.do")
	public ModelAndView login(String member_id, String member_password,HttpSession session) throws Exception{
		ModelAndView mv = null;
		try{
			MemberDTO mdto = service.getMemberById(member_id);
			System.out.println(mdto);
			if(mdto!=null){
				// 패스워드 비교
				if(mdto.getMember_password().equals(member_password)){
					session.setAttribute("login_info", mdto);
					mv = new ModelAndView("main.jsp");
				}else{// 패스워드가 틀린 경우
					mv =  new ModelAndView("login_form.jsp","error_message","wrong password!");
				}
			}else{// id 가 없는 경우
				return new ModelAndView("login_form.jsp","error_message","No Id");
			}
		}catch (SQLException e) {
			e.printStackTrace();
			return new ModelAndView("error.res","error_message",e.getMessage());
		}
		return mv;
	}
	// 로그아웃
	@RequestMapping("logout.do")
	public ModelAndView logout(HttpSession session) throws Exception{
		session.invalidate();
		return new ModelAndView("main.jsp");
	}
	// 프로필 상세
	@RequestMapping("member_detail.do")
	public ModelAndView member_detail() throws Exception{
		ModelAndView mv;
		try{
			mv = new ModelAndView("member_detail.sub");
		}catch (Exception e) {
			e.printStackTrace();
			return new ModelAndView("error.res","error_message",e.getMessage());
		}
		return mv;
	}
	// 회원 정보 수정 폼 호출
	@RequestMapping("member_update_form.do")
	public ModelAndView update_form(HttpSession session) throws Exception{
		ModelAndView mv;
		try{
			MemberDTO mdto = (MemberDTO) session.getAttribute("login_info");
			mdto = service.getMemberById(mdto.getMember_id());
			mv = new ModelAndView("member_update_form.sub","member",mdto);
		}
		catch (Exception e) {
			e.printStackTrace();
			return mv = new ModelAndView("error.res","error_message",e.getMessage());
		}
		return mv;
	}
	// 회원 정보 수정
	@RequestMapping("member_update_done.do")
	public ModelAndView member_update(HttpServletRequest request, HttpSession session, MemberDTO new_mdto) throws Exception{
		ModelAndView mv;
		try{
			// 생년월일 변환
			String year = request.getParameter("birth_year");
			String month = request.getParameter("birth_month"); 
			String day = request.getParameter("birth_day");
			System.out.println(year+","+month+","+day);
			if(month.length()!=2){
				month = "0".concat(month);
			}
			if(day.length()!=2){
				day = "0".concat(day);
			}
			// 생년월일 저장
			new_mdto.setBirthday(year+month+day);
			
			MemberDTO old_mdto = (MemberDTO)session.getAttribute("login_info");
			System.out.println("old_mdto" + old_mdto);	
			System.out.println("new_mdto" + new_mdto);
			// update 처리 후 바뀐 정보를 리턴 받는다.
			new_mdto = service.updateMember(old_mdto,new_mdto);
			// 바뀐 정보를 session에 바인딩
			session.setAttribute("login_info", new_mdto);
			mv = new ModelAndView("member_detail.sub");
		}catch (Exception e) {
			e.printStackTrace();
			return mv = new ModelAndView("error.res","error_message",e.getMessage());
		}
		return mv;
	}
}
